﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Jzy.Model.DocManger;
using Jzy.BLL.DocManger;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.Right;
using Frame.Lib.Core.Dept;
using Jzy.DAL;


namespace Jzy.Web.DocManager.Dir
{
    public partial class Edit : PageEdit
    {
        private DirBO _dir;
        private DirBO Dir
        {
            get
            {
                if (_dir == null)
                {
                    _dir = new DirBO();
                }
                return _dir;
            }
        }

        private string UpperID
        {
            get
            {
                if (string.IsNullOrEmpty(Request["UpperID"]))
                {
                    return null;
                }
                else
                {

                    return this.Decrypt(Request["UpperID"]);
                }
            }
        }

        private bool IsPrivate
        {
            get
            {
                if (string.IsNullOrEmpty(Request["IsPrivate"]))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                WebUtil.BindListControlWithEnum(TypeID, typeof(DirType));
                if (!string.IsNullOrEmpty(this.PKID))
                {
                    DispInfo();
                }
                else
                {
                    hidUpperID.Value = this.UpperID;
                    lblUpperName.Text = Dir.GetName(int.Parse(hidUpperID.Value));
                    Code.Text = Dir.GetMaxCode().ToString();
                    txtOrders.Text = Dir.GetMaxOrder().ToString();
                }
            }
        }

        private void DispInfo()
        {
            DirInfo info = Dir.GetInfo(this.PKID);
            txtName.Text = info.Name.ToString();
            txtOrders.Text = info.Orders.ToString();
            Code.Text = info.Code.ToString();
            TypeID.SelectedValue = info.TypeID.ToString();

            hidUpperID.Value = info.UpperID.ToString();
            if ((int)info.UpperID == 0)
            {
                lblUpperName.Text = "顶级目录";
                lblUpperName.Enabled = false;
            }
            else
            {
                lblUpperName.Text = Dir.GetName((int)info.UpperID);
            }
            txtRemark.Text = info.Remark.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strName = txtName.Text.Trim();
            int upperID = int.Parse(hidUpperID.Value);
            DirInfo info = Dir.GetInfo(this.PKID);
            if (string.IsNullOrEmpty(this.PKID))
            {
                info.IsPrivate = IsPrivate;
            }
            bool flag = Dir.Exist(upperID, strName, this.PKID, info.IsPrivate) || Dir.ExistCode(Code.Text.Trim(), PKID);//code要求全局唯一
            if (flag)
            {
                int idTmp = int.Parse(this.PKID);
                if (idTmp == 0)
                {
                    idTmp = Dir.GetID(upperID, strName);
                }
                //判断存在的目录该用户有没有浏览权限
                RecordRightBO right = new RecordRightBO(OTableName.DocDirRight);

                bool haveRight = right.HaveRight(idTmp, AppSession.UserID, RecordRightName.Browse, RightOwnerType.User);
                string strMessage = null;
                if (haveRight || AppSession.UserID == Constants.ADMIN_ID)
                {
                    strMessage = "该目录或编号已存在";
                }
                else
                {
                    strMessage = "该目录或编号已存在\\n您没有浏览权限\\n请与管理员联系";
                }

                JSUtil.Alert(strMessage);
            }
            else
            {
                
                info.Name = txtName.Text.Trim();
                if (txtOrders.Text.Trim() != "")
                {
                    info.Orders = int.Parse(txtOrders.Text);
                }
                info.Code = int.Parse(Code.Text);
                info.TypeID = int.Parse(TypeID.SelectedValue);
                info.Remark = txtRemark.Text.Trim();



                if (this.PKID == null)//修改时不改变创建者
                {
                    info.IsPrivate = IsPrivate;
                    info.CreatorID = AppSession.UserID;
                    info.CreatorName = AppSession.UserName;
                    info.InputTime = DateTime.Now;

                    DirInfo upperInfo = (DirInfo)Dir.GetInfo(upperID);
                    info.UpperID = upperID;
                    info.Depth = (int)upperInfo.Depth + 1;
                    info.Path = upperInfo.Path.ToString() + upperInfo.ID + ",";
                }

                Dir.Save(info);

                //处理权限
                if (this.PKID == null)
                {
                    int bindID = (int)info.ID;

                    RecordRightBO right = new RecordRightBO(OTableName.DocDirRight);

                    DeptBO dept = new DeptBO();
                    int rootID = (int)dept.GetRootID();
                    List<int> lstDeptIDs = new List<int>();
                    lstDeptIDs.Add(rootID);

                    //浏览            
                    right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Browse, RightOwnerType.Dept);

                    //增加
                    right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Add, RightOwnerType.Dept);

                    //拥有者
                    right.SaveOwnerIDs(new List<int>(new int[] { AppSession.UserID }), bindID, RecordRightName.Creator, RightOwnerType.User);

                }

                string strScript = "top.document.getElementsByName('frameLeft')[0].contentWindow.document.getElementById('ctl00_ContentPlaceHolder1_btnRefresh').click();";
                strScript += "top.refreshView(false);top.delDivFrm();";
                JSUtil.ResponseJS(strScript);
            }
        }
    }
}
